/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.answers;

import com.hyts.algorithm.Solution;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.answers
 * @author:LiBo/Alex
 * @create-date:2019-11-01 11:17
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class day02 implements Solution {


    //TODO 去重算法 大神篇
    public static List<Integer> findDuplicates(int[] nums) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length; ++i) {
            int index = Math.abs(nums[i])-1;
            if (nums[index] < 0) {
                res.add(Math.abs(index + 1));
            }
            nums[index] = -nums[index];
        }
        return res;
    }

    /**
     * TODO 待解决 中位数算法
     */
    @Override
    public void solution1() {

        int []  a = {1,3,4,5,6,7};
        int []  b = {2,4,6,8,10};

        if((a.length&1) != 0 && (b.length&1) != 0 || (a.length&1) == 0 && (b.length&1) == 0){
            int length = a.length + b.length;
            int num = length/2;
            //TODO
        }else{
            int length = a.length + b.length;
            if(a[length-1]<=b[0]){
                int index = length/2-1;
                if(index > a.length-1){
                    index -= (a.length+1);
                }
                System.out.println(a[index]);
                return ;
            }
            if(b[length-1]<=a[0]){
                int index = length/2-1;
                if(index > b.length-1){
                    index -= (b.length+1);
                }
                System.out.println(a[index]);
                return ;
            }
        }
    }

    /**
     * TODO
     * 回文数列
     * 计算最长回文数列算法，并且输出来 可能包含多个哦
     * S最大可以为1000个
     */

    @Test
    @Override
    public void solution2() {
        String testStr = "abbabbabb";

        StringBuilder stringBuilder = new StringBuilder();

        ArrayList<String> stringList = new ArrayList<>();

        class PreCheck{

            int start = 0 ;

            int end  = 0;

            //acbcccba

            StringBuilder buffer = new StringBuilder();

            void check(Character character){
                buffer.append(character);
                String dealStr = buffer.toString();
                for(Character character1:dealStr.toCharArray()){
                    /*if(end <= dealStr.length()-1*//* && character1.charValue() == dealStr.toCharArray()[++end]*//*){*/
                        char[] dats = dealStr.toCharArray();
                        start = 0;
                        end = dats.length-1;
                        int initEnd = end;
                        boolean recycle = false;
                        while (end - start > 2) {
                                if (dats[start] == dats[end]) {
                                    if(recycle){
                                        end = initEnd;
                                    }
                                    start++;
                                    end--;
                                    recycle = false;
                                }else if(recycle){
                                    end--;
                                }else {
                                    initEnd = dats.length - 1;
                                    end = initEnd;
                                    recycle = true;
                                }
                        }
                        if (end - start <= 2 && initEnd - start > 1) {
                            if(dats[start] == dats[initEnd]){
                                stringList.add(buffer.substring(start, initEnd+1)+",initEnd:"+initEnd+"--"+start);
                            }
                        }

                        /*for(Character character2 : dats){
                            if(character2.charValue() == character1){
                                if(end - start<=2 && ! stringList.contains(buffer.substring(start,end))){
                                    stringList.add(buffer.substring(start,end));
                                }else{
                                    while(end - start>2){

                                    }
                                }
                            }
                            end++;
                        }*/
                   // }
                }
            }
        }
        PreCheck preCheck = new PreCheck();
        for(Character character : testStr.toCharArray()){
            preCheck.check(character);
        }
        System.out.println(stringList);

    }

    @Test
    @Override
    public void solution3() {


    }


}
